package com.gxks.lhs.controller.user;


import com.azure.core.annotation.Put;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gxks.lhs.DTO.hotSearchDTO;
import com.gxks.lhs.constants.R;
import com.gxks.lhs.entity.Goods;
import com.gxks.lhs.security.SecurityUtils;
import com.gxks.lhs.service.IGoodsService;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Parameter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 商品表 前端控制器
 * </p>
 *
 * @author LuoHaisong
 * @since 2023-06-02
 */
@RestController
@RequestMapping("/restful-api/goods")
public class UserGoodsController {
    @Autowired
    private IGoodsService goodsService;

    @ApiOperation("获取最近发布的二手手机")
    @GetMapping("/list")
    public R getGoodsList(@RequestParam(value = "categoryId", required = false, defaultValue = "0") Integer categoryId,
                          @RequestParam(value = "current", required = false, defaultValue = "1") Integer current,
                          @RequestParam(value = "size", required = false, defaultValue = "10") Integer size
    ) {
        Page<Goods> goodsPage = goodsService.findGoodsList(categoryId, current, size);
        if (goodsPage != null)
            return R.success(goodsPage, "获取成功");
        return R.error("获取失败");
    }

    @ApiOperation("发布二手手机")
    @PostMapping("/addGoods")
    public R addGoods(@RequestBody Goods goods) {
        goods.setId(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS")) + Math.round(Math.random() * 100000));
        String userId = SecurityUtils.getCurrentUserId();
        goods.setUserId(userId);
        goods.setStatus(1);
        if (goods.getCategoryId() == null || goods.getCategoryId() == 0 || goods.getPrice() == null || goods.getTitle() == null || goods.getStorage() == null || goods.getDegree() == null || goods.getDescription() == null)
            return R.error("发布失败，请检查后内容是否填写完整！");
        if (goodsService.addGoods(goods))
            return R.success().setMsg("发布成功");
        return R.error("发布二手手机失败，请重试");
    }

    @ApiOperation("根据id获取二手手机的详细信息")
    @GetMapping("/getGoods")
    public R getGoodsById(@RequestParam String id) {
        Goods goods = goodsService.findGoodsById(id);
        if (goods != null)
            return R.success(goods, "获取成功");
        return R.error("获取失败");
    }


    @ApiOperation("更新二手手机信息")
    @PutMapping("/updateGoods")
    public R updateGoods(@RequestBody Goods goods) {
        String userId = SecurityUtils.getCurrentUserId();
        goods.setUserId(userId);
        if (goodsService.updateGoodsforUser(goods)) {
            return R.success().setMsg("更新成功");
        }
        return R.error("更新成功");
    }

    @ApiOperation("根据用户id获取发布的二手手机信息")
    @GetMapping("/getGoodsByUserId")
    public R getGoodsByUserId(@RequestParam(value = "current", required = false, defaultValue = "1") Integer current,
                              @RequestParam(value = "size", required = false, defaultValue = "10") Integer size) {
        String userId = SecurityUtils.getCurrentUserId();
        Page<Goods> goods = goodsService.findGoodsByUserId(userId, current, size);
        if (goods != null)
            return R.success(goods, "获取成功");
        return R.error("获取失败");
    }

    @ApiOperation("下架二手手机")
    @PutMapping("/soldOut")
    public R soldOut(@RequestParam String goodsId) {
        String userId = SecurityUtils.getCurrentUserId();
        if (goodsService.soldOut(userId, goodsId))
            return R.success().setMsg("下架成功");
        return R.error("下架失败");
    }

    @ApiOperation("上架二手手机")
    @PutMapping("/soldIn")
    public R soldIn(@RequestParam String goodsId) {
        String userId = SecurityUtils.getCurrentUserId();
        if (goodsService.soldIn(userId, goodsId))
            return R.success().setMsg("上架成功");
        return R.error("上架失败");
    }

    @ApiOperation("删除自己发布的二手手机")
    @DeleteMapping("/deleteGoods")
    public R deleteGoods(@RequestParam String goodsId) {
        String userId = SecurityUtils.getCurrentUserId();
        if (goodsService.removeGoods(userId, goodsId))
            return R.success().setMsg("删除成功");
        return R.error("删除失败");
    }

    @ApiOperation("随机获取手机列表")
    @GetMapping("/getGoodsList")
    public R getGoodsList() {
        List<Goods> goods = goodsService.randomFindGoodsList();
        if (goods != null)
            return R.success(goods, "获取成功");
        return R.error("获取失败");
    }

    @ApiOperation("获取热搜列表")
    @GetMapping("/hotSearch")
    public R hotSearch() {
        List<hotSearchDTO> hotSearch = new ArrayList<hotSearchDTO>();
        hotSearch.add(new hotSearchDTO("xiaomi 18标题", "xiaomi 18"));
        hotSearch.add(new hotSearchDTO("苹果-标题", "苹果18"));

        if (hotSearch != null)
            return R.success(hotSearch, "获取成功");
        return R.error("获取失败");
    }

    @ApiOperation("搜索二手手机")
    @GetMapping("/search")
    public R search(@RequestParam(value = "prodName", required = false, defaultValue = "") String prodName,
                    @Parameter(name = "sort", description = "排序 0 默认最新发布 1价格排序升序 2 价格降序 3 最近更新") String sort,
                    @RequestParam(value = "current", required = false, defaultValue = "1") Integer current,
                    @RequestParam(value = "size", required = false, defaultValue = "10") Integer size) {
        Page<Goods> goods = goodsService.userSearch(prodName, sort, current, size);
        if (goods != null)
            return R.success(goods, "获取成功");
        return R.error("获取失败");
    }

    @ApiOperation("更加用户id获取用户已发布未卖出的二手时间")
    @GetMapping("/getMyGoods")
    public R getMyGoods(@RequestParam(value = "current", required = false, defaultValue = "1") Integer current,
                        @RequestParam(value = "size", required = false, defaultValue = "10") Integer size) {
        String userId = SecurityUtils.getCurrentUserId();
        Page<Goods> goods = goodsService.findGoodsByUserId(userId, current, size);
        return R.success(goods, "获取成功");
    }

}

